home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / comm / tcp / IPDial1_3.lha / ipdial / IPDial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-03  |  15.4 KB  |  835 lines

  1. /**
  2. ***  IPDial    Script program for initializing a SLIP connection
  3. ***  Copyright    (C)   1994    Jochen Wiedmann
  4. ***
  5. ***  This program is free software; you can redistribute it and/or modify
  6. ***  it under the terms of the GNU General Public License as published by
  7. ***  the Free Software Foundation; either version 2 of the License, or
  8. ***  (at your option) any later version.
  9. ***
  10. ***  This program is distributed in the hope that it will be useful,
  11. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ***  GNU General Public License for more details.
  14. ***
  15. ***  You should have received a copy of the GNU General Public License
  16. ***  along with this program; if not, write to the Free Software
  17. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ***
  19. ***
  20. ***  This is the main part of the program.
  21. ***
  22. ***
  23. ***  Computer: Amiga 1200            Compiler: Dice 3.01
  24. ***
  25. ***  Author:    Jochen Wiedmann
  26. ***        Am Eisteich 9
  27. ***        72555 Metzingen
  28. ***        Germany
  29. ***
  30. ***        Phone: (+0049) 7123 / 14881
  31. ***        Internet: jochen.wiedmann@uni-tuebingen.de
  32. ***
  33. ***
  34. ***  History:    V 1.1    23.11.94    Initial version
  35. ***
  36. ***        V 1.2    27.02.95    Added terminal mode
  37. ***                    Now using ReadArgs() for
  38. ***                    command line parsing.
  39. ***
  40. ***        V 1.3    09.03.95    Added environment variable
  41. ***                    aupport to "send" command.
  42. ***                    Added unit support.
  43. **/
  44.  
  45.  
  46.  
  47.  
  48.  
  49. #define VERSION     1
  50. #define REVISION    3
  51. #define VSTRING     "IPDial 1.3 (09.03.95)"
  52. #define VERSTAG     "\0$VER: IPDial 1.3 (09.03.95)"
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. /**
  60. ***  Include files
  61. **/
  62. #ifndef IPDIAL_H
  63. #include "IPDial.h"
  64. #endif
  65.  
  66. #include <ctype.h>
  67. #include <clib/alib_protos.h>
  68.  
  69.  
  70.  
  71.  
  72.  
  73. /**
  74. ***  This structure describes one command. All commands are stored
  75. ***  in a table at the end of the file.
  76. **/
  77. struct ScriptLine;
  78. typedef VOID (*CommandFunc) (struct ScriptLine *);
  79. struct Command
  80. { CommandFunc Func;
  81.   STRPTR Name;
  82. };
  83.  
  84.  
  85.  
  86.  
  87.  
  88. /**
  89. ***  Each line of the script file is stored in a structure like below.
  90. **/
  91. struct ScriptLine
  92. { struct MinNode mn;
  93.   ULONG Num;
  94.   CommandFunc CommFunc;
  95.   STRPTR Label;
  96.   STRPTR Args;
  97. };
  98.  
  99.  
  100.  
  101.  
  102.  
  103. /**
  104. ***  Global variables
  105. **/
  106. LONG StatusVar;
  107.  
  108. struct MinList ScriptLineList;
  109.  
  110. struct ScriptLine *CurrentScriptLine;
  111.  
  112. const UBYTE VersTag [] = VERSTAG;
  113. const UBYTE VString [] = VSTRING;
  114.  
  115. ULONG EchoMode = FALSE;
  116. ULONG VerboseMode = FALSE;
  117.  
  118. STRPTR SerialDeviceName = NULL;
  119.  
  120. struct RDArgs *MainRDArgs = NULL;
  121.  
  122.  
  123.  
  124.  
  125.  
  126. /**
  127. ***  This function is used to skip blanks.
  128. **/
  129. STRPTR SkipBlanks(const UBYTE *ptr)
  130.  
  131. { while(*ptr == ' '  ||  *ptr == '\t')
  132.   { ++ptr;
  133.   }
  134.   return((STRPTR) ptr);
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141. /**
  142. ***  This function is used to parse a string for characters
  143. ***  like '\r' or '\n'.
  144. **/
  145. STRPTR ParseString(const UBYTE *ptr)
  146.  
  147. { STRPTR dup, result;
  148.  
  149.   if (!(dup = malloc(strlen((char *) ptr)+1)))
  150.   { perror("malloc");
  151.     exit(10);
  152.   }
  153.   result = dup;
  154.  
  155.   while(*ptr)
  156.   { if (*ptr == '\\')
  157.     { ++ptr;
  158.       switch(*ptr)
  159.       { case 'r':
  160.       *dup++ = '\r';
  161.       break;
  162.     case 'n':
  163.       *dup++ = '\n';
  164.       break;
  165.     default:
  166.       *dup++ = *ptr;
  167.       break;
  168.       }
  169.     }
  170.     else
  171.     { *dup++ = *ptr;
  172.     }
  173.     ++ptr;
  174.   }
  175.  
  176.   *dup = '\0';
  177.  
  178.   return(result);
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185. /**
  186. ***  This is an empty function. Just to allow lines with labels only.
  187. **/
  188. VOID NoneFunc(struct ScriptLine *line)
  189.  
  190. {
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197. /**
  198. ***  This is the echo function.
  199. **/
  200. VOID EchoFunc(struct ScriptLine *line)
  201.  
  202. { STRPTR *args;
  203.   int i;
  204.  
  205.   if (!StrReadArgs(line->Args, (LONG *) &args, (STRPTR) "ARGS/M"))
  206.   { fprintf(stderr, "Line %ld: Argument or memory error\n", line->Num);
  207.     exit(10);
  208.   }
  209.  
  210.   for(i = 0;  *args;  ++i, ++args)
  211.   { if (i)
  212.     { putchar(' ');
  213.     }
  214.     fputs((char *) ParseString(*args), stdout);
  215.   }
  216. }
  217.  
  218.  
  219.  
  220.  
  221.  
  222. /**
  223. ***  This is the device function.
  224. **/
  225. VOID DeviceFunc(struct ScriptLine *line)
  226.  
  227. { struct
  228.   { STRPTR Device;
  229.     STRPTR Protocol;
  230.     LONG *Unit;
  231.   } args;
  232.  
  233.   if (SerialDeviceName)
  234.   { fprintf(stderr, "Line %ld: Device already open, ignoring.\n", line->Num);
  235.   }
  236.  
  237.   args.Unit = NULL;
  238.   args.Device = NULL;
  239.   args.Protocol = NULL;
  240.  
  241.   if (!StrReadArgs(line->Args, (LONG *) &args,
  242.            (STRPTR) "DEVICE,PROTOCOL,UNIT/K/N"))
  243.   { fprintf(stderr, "Line %ld: Argument or memory error\n", line->Num);
  244.     exit(10);
  245.   }
  246.  
  247.   if (!args.Device)
  248.   { fprintf(stderr, "Line %ld: Missing device name.\n", line->Num);
  249.     exit(10);
  250.   }
  251.   if (!(SerialDeviceName = strdup((char *) args.Device)))
  252.   { perror("malloc");
  253.     exit(10);
  254.   }
  255.  
  256.   if (VerboseMode)
  257.   { printf("Opening %s.\n", args.Device);
  258.   }
  259.  
  260.   if (!SerialOpen(args.Device, args.Protocol, args.Unit ? *args.Unit : 0))
  261.   { fprintf(stderr, "Line %ld: Unknown protocol.\n", line->Num);
  262.     exit(10);
  263.   }
  264. }
  265.  
  266.  
  267.  
  268.  
  269.  
  270. /**
  271. ***  This function is used to set the serial.device parameters.
  272. **/
  273. VOID SetFunc(struct ScriptLine *line)
  274.  
  275. { struct
  276.   { ULONG *Baud;
  277.     ULONG *DataBits;
  278.     ULONG *StopBits;
  279.     ULONG *BufSize;
  280.     STRPTR Parity;
  281.     STRPTR Protocol;
  282.   } args;
  283.  
  284.   if (!SerialDeviceName)
  285.   { fprintf(stderr, "Line %ld: Must open serial.device first.\n", line->Num);
  286.     exit(10);
  287.   }
  288.  
  289.   args.Baud = NULL;
  290.   args.DataBits = NULL;
  291.   args.StopBits = NULL;
  292.   args.BufSize = NULL;
  293.   args.Parity = NULL;
  294.   args.Protocol = NULL;
  295.  
  296.   if (!(StrReadArgs(line->Args, (LONG *) &args,
  297.             (STRPTR) "BAUD/K/N,DATABITS/K/N,STOPBITS/K/N,BUFSIZE/K/N,"
  298.                  "PARITY/K,PROTOCOL/K")))
  299.   { fprintf(stderr, "Line %ld: Argument or memory error.\n", line->Num);
  300.     exit(10);
  301.   }
  302.  
  303.   if (args.Baud)
  304.   { SerialSetBaud(*args.Baud);
  305.   }
  306.   if (args.DataBits)
  307.   { SerialSetDataBits(*args.DataBits);
  308.   }
  309.   if (args.StopBits)
  310.   { SerialSetStopBits(*args.StopBits);
  311.   }
  312.   if (args.BufSize)
  313.   { SerialSetBufSize(*args.BufSize);
  314.   }
  315.   if (args.Parity)
  316.   { if (!SerialSetParity(args.Parity))
  317.     { fprintf(stderr, "Line %ld: Unknown parity.\n", line->Num);
  318.       exit(10);
  319.     }
  320.   }
  321.   if (args.Protocol)
  322.   { if (!SerialSetProtocol(args.Protocol))
  323.     { fprintf(stderr, "Line %ld: Unknown protocol.\n", line->Num);
  324.       exit(10);
  325.     }
  326.   }
  327.  
  328.   if (VerboseMode)
  329.   { printf("%s Parameters modified:\n\n", SerialDeviceName);
  330.     SerialShowParms();
  331.   }
  332. }
  333.  
  334.  
  335.  
  336.  
  337.  
  338. /**
  339. ***  This function shows the serial.device parameters.
  340. **/
  341. VOID ShowParmsFunc(struct ScriptLine *line)
  342.  
  343. { if (!SerialDeviceName)
  344.   { fprintf(stderr, "Line %ld: Must open serial.device first.\n", line->Num);
  345.     exit(10);
  346.   }
  347.  
  348.   printf("%s Parameters:\n\n", SerialDeviceName);
  349.   SerialShowParms();
  350. }
  351.  
  352.  
  353.  
  354.  
  355.  
  356. /**
  357. ***  This function sends a string to the serial.device.
  358. **/
  359. VOID SendFunc(struct ScriptLine *line)
  360.  
  361. { STRPTR *args;
  362.  
  363.   if (!SerialDeviceName)
  364.   { fprintf(stderr, "Line %ld: Must open serial.device first.\n", line->Num);
  365.     exit(10);
  366.   }
  367.  
  368.   if (!StrReadArgs(line->Args, (LONG *) &args, (STRPTR) "ARGS/M"))
  369.   { fprintf(stderr, "Line %ld: Argument or memory error\n", line->Num);
  370.     exit(10);
  371.   }
  372.  
  373.   if (VerboseMode)
  374.   { STRPTR *vargs = args;
  375.  
  376.     printf("\nSending ");
  377.     for (;  *vargs;  ++vargs)
  378.     { STRPTR send = ParseString(*args);
  379.  
  380.       printf("%s", send);
  381.     }
  382.     printf("\n");
  383.   }
  384.   for(;  *args;  args++)
  385.   { STRPTR send = ParseString(*args);
  386.  
  387.     SerialSend(send, strlen((char *) send));
  388.   }
  389. }
  390.  
  391.  
  392.  
  393.  
  394.  
  395. /**
  396. ***  This is similar to "if".
  397. **/
  398. VOID DoGoto(STRPTR Label, int Num)
  399.  
  400. { struct ScriptLine *sl;
  401.  
  402.   for(sl = (struct ScriptLine *) ScriptLineList.mlh_Head;
  403.         sl->mn.mln_Succ;
  404.         sl = (struct ScriptLine *) sl->mn.mln_Succ)
  405.   { if (sl->Label  &&  strcmp((char *) sl->Label, (char *) Label) == 0)
  406.     { CurrentScriptLine = (struct ScriptLine *) sl->mn.mln_Pred;
  407.       return;
  408.     }
  409.   }
  410.  
  411.   fprintf(stderr, "Line %ld: Unknown label.\n", Num);
  412.   exit(10);
  413. }
  414. VOID OnFunc(struct ScriptLine *line)
  415.  
  416. { STRPTR ptr = line->Args;
  417.  
  418.   if (strnicmp((char *) ptr, "status", 6) == 0)
  419.   { ptr = SkipBlanks(ptr+6);
  420.     if (strnicmp((char *) ptr, "goto", 4) == 0)
  421.     { STRPTR *labels;
  422.       int i;
  423.  
  424.       if (!StrReadArgs(ptr+4, (LONG *) &labels, "LABELS/M"))
  425.       { fprintf(stderr, "Line %ld: Memory error.\n");
  426.     exit(10);
  427.       }
  428.       for (i = -1;  *labels;  ++i, ++labels)
  429.       { if (i == StatusVar)
  430.     { DoGoto(*labels, line->Num);
  431.       return;
  432.     }
  433.       }
  434.       return;
  435.     }
  436.   }
  437.  
  438.   fprintf(stderr, "Line %ld: Condition syntax.", line->Num);
  439.   exit(10);
  440. }
  441.  
  442.  
  443.  
  444.  
  445.  
  446. /**
  447. ***  This function waits for certain strings.
  448. **/
  449. VOID WaitFunc(struct ScriptLine *line)
  450.  
  451. { struct
  452.   { STRPTR *WaitArgs;
  453.     ULONG *TimeOut;
  454.   } args;
  455.   LONG TimeOut = -1;
  456.  
  457.   if (!SerialDeviceName)
  458.   { fprintf(stderr, "Line %ld: Must open serial.device first.\n", line->Num);
  459.     exit(10);
  460.   }
  461.  
  462.   args.WaitArgs = NULL;
  463.   args.TimeOut = NULL;
  464.  
  465.   if (!(StrReadArgs(line->Args, (LONG *) &args, (STRPTR) "ARGS/M,TIMEOUT/N/K")))
  466.   { fprintf(stderr, "Line %ld: Argument or memory error.\n", line->Num);
  467.     exit(10);
  468.   }
  469.  
  470.   if (!args.WaitArgs)
  471.   { fprintf(stderr, "Line %ld: Missing argument.\n", line->Num);
  472.     exit(10);
  473.   }
  474.   if (!args.TimeOut)
  475.   { fprintf(stderr, "Line %ld: Missing timeout argument.\n", line->Num);
  476.     exit(10);
  477.   }
  478.   TimeOut = *args.TimeOut;
  479.  
  480.   StatusVar = SerialWait(args.WaitArgs, TimeOut);
  481. }
  482.  
  483.  
  484.  
  485.  
  486.  
  487. /**
  488. ***  The delay function waits for a certain amount of time.
  489. **/
  490. VOID DelayFunc(struct ScriptLine *line)
  491.  
  492. { ULONG secs = atol((char *) line->Args);
  493.  
  494.   if (secs == 0)
  495.   { fprintf(stderr, "Line %ld: Missing argument.\n", line->Num);
  496.     exit(10);
  497.   }
  498.  
  499.   Delay(secs * 50);
  500. }
  501.  
  502.  
  503.  
  504.  
  505.  
  506. /**
  507. ***  And this is the Exit function.
  508. **/
  509. VOID ExitFunc(struct ScriptLine *line)
  510.  
  511. { LONG result = atol((char *) line->Args);
  512.  
  513.   exit(result);
  514. }
  515.  
  516.  
  517.  
  518.  
  519.  
  520. /**
  521. ***  The Goto command
  522. **/
  523. VOID GotoFunc(struct ScriptLine *line)
  524.  
  525. { STRPTR Label;
  526.  
  527.   if (!(StrReadArgs(line->Args, (LONG *) &Label, "LABEL/A")))
  528.   { fprintf(stderr, "Line %ld: Missing argument or memory error.\n",
  529.         line->Num);
  530.     exit(10);
  531.   }
  532.   DoGoto(Label, line->Num);
  533. }
  534.  
  535.  
  536.  
  537.  
  538.  
  539. /**
  540. ***  This function enters terminal mode.
  541. **/
  542. VOID TermFunc(struct ScriptLine *line)
  543.  
  544. { SerialTerminal();
  545. }
  546.  
  547.  
  548.  
  549.  
  550.  
  551. /**
  552. ***  This is the command table.
  553. **/
  554. struct Command CommandTab[] =
  555. { { EchoFunc,        (STRPTR) "echo"         },
  556.   { DeviceFunc,     (STRPTR) "device"       },
  557.   { SetFunc,        (STRPTR) "set"          },
  558.   { ShowParmsFunc,  (STRPTR) "showparms"    },
  559.   { SendFunc,        (STRPTR) "send"         },
  560.   { OnFunc,        (STRPTR) "on"           },
  561.   { WaitFunc,        (STRPTR) "wait"         },
  562.   { DelayFunc,        (STRPTR) "delay"        },
  563.   { ExitFunc,        (STRPTR) "exit"         },
  564.   { GotoFunc,        (STRPTR) "goto"         },
  565.   { TermFunc,        (STRPTR) "terminal"     },
  566.   { NULL,        NULL            }
  567. };
  568.  
  569.  
  570.  
  571.  
  572.  
  573. /**
  574. ***  This function is used to process the file. Rather easy,
  575. ***  isn't it? :-)
  576. **/
  577. VOID ProcessFile(VOID)
  578.  
  579. { for(CurrentScriptLine = (struct ScriptLine *) ScriptLineList.mlh_Head;
  580.       CurrentScriptLine->mn.mln_Succ;
  581.       CurrentScriptLine = (struct ScriptLine *) CurrentScriptLine->mn.mln_Succ)
  582.   { (*CurrentScriptLine->CommFunc)(CurrentScriptLine);
  583.   }
  584. }
  585.  
  586.  
  587.  
  588.  
  589.  
  590. /**
  591. ***  This function reads and parses the file.
  592. **/
  593. VOID ParseFile(STRPTR file)
  594.  
  595. { FILE *fp;
  596.   STATIC UBYTE buffer[4096];
  597.   ULONG linenum = 0;
  598.   ULONG success = TRUE;
  599.  
  600.   if (!(fp = fopen((char *) file, "r")))
  601.   { fprintf(stderr, "Could not open %s for reading.\n", file);
  602.     exit(10);
  603.   }
  604.  
  605.   NewList((struct List *) &ScriptLineList);
  606.  
  607.   while(fgets((char *) buffer, sizeof(buffer), fp))
  608.   { ULONG len = strlen((char *) buffer);
  609.     STRPTR line;
  610.     STRPTR Label = NULL;
  611.     CommandFunc CommFunc;
  612.  
  613.     ++linenum;
  614.  
  615.     if (buffer[len-1] != '\n')
  616.     { fprintf(stderr, "Line %ld too long.\n", linenum);
  617.       exit(10);
  618.     }
  619.  
  620.     if (!(line = malloc(len+1)))
  621.     { perror("malloc");
  622.       exit(10);
  623.     }
  624.     strcpy(line, buffer);
  625.     line = SkipBlanks(line);
  626.  
  627.     /**
  628.     ***  Check for a label
  629.     **/
  630.     { STRPTR ptr = line;
  631.  
  632.       if (isalpha(*ptr))
  633.       { do
  634.     { ++ptr;
  635.     }
  636.     while(isalnum(*ptr));
  637.  
  638.     if (*ptr == ':')
  639.     { *ptr = '\0';
  640.       Label = line;
  641.       line = SkipBlanks(ptr+1);
  642.     }
  643.       }
  644.     }
  645.  
  646.     /**
  647.     ***  Check for empty line or comment line
  648.     **/
  649.     if (*line == ';'  ||  *line == '\r'  ||  *line == '\n')
  650.     { CommFunc = NoneFunc;
  651.       if (!Label)
  652.       { continue;
  653.       }
  654.     }
  655.  
  656.     /**
  657.     ***  If no empty line: Check for command
  658.     **/
  659.     else
  660.     { struct Command *comm;
  661.  
  662.       for (comm = &CommandTab[0];  comm->Func;  ++comm)
  663.       { ULONG len = strlen((char *) comm->Name);
  664.  
  665.     if (strnicmp((char *) comm->Name, (char *) line, len) == 0  &&
  666.         (line[len] == ' '  ||  line[len] == '\t'  ||
  667.          line[len] == '\r' ||  line[len] == '\n'))
  668.     { line = SkipBlanks(line+len);
  669.       CommFunc = comm->Func;
  670.       break;
  671.     }
  672.       }
  673.  
  674.       if (!comm->Func)
  675.       { fprintf(stderr, "Line %ld: Unknown command.\n", linenum);
  676.     success = FALSE;
  677.       }
  678.     }
  679.  
  680.     /**
  681.     ***  Allocate a new scriptline structure.
  682.     **/
  683.     { struct ScriptLine *sl;
  684.  
  685.       if (!(sl = malloc(sizeof(*sl))))
  686.       { perror("malloc");
  687.     exit(10);
  688.       }
  689.  
  690.       AddTail((struct List *) &ScriptLineList, (struct Node *) sl);
  691.       sl->Num = linenum;
  692.       sl->CommFunc = CommFunc;
  693.       sl->Args = line;
  694.       sl->Label = Label;
  695.     }
  696.   }
  697.  
  698.   if (ferror(fp))
  699.   { perror("fgets");
  700.     exit(10);
  701.   }
  702.  
  703.   if (!success)
  704.   { exit(10);
  705.   }
  706.  
  707.   fclose(fp);
  708. }
  709.  
  710.  
  711.  
  712.  
  713.  
  714. /**
  715. ***  This is the Cleanup() function, called, when the program terminates.
  716. **/
  717. VOID Cleanup(VOID)
  718.  
  719. { SerialCleanup();
  720.   StrReadArgsFree();
  721.   if (MainRDArgs)
  722.   { FreeArgs(MainRDArgs);
  723.   }
  724. }
  725.  
  726.  
  727.  
  728.  
  729.  
  730. /**
  731. ***  This function hints about the GPL.
  732. **/
  733. VOID ShowGPL(FILE *fp)
  734.  
  735. { fprintf(fp, "This program is governed by the terms and conditions of the\n");
  736.   fprintf(fp, "GNU General Public License. A copy should have come with\n");
  737.   fprintf(fp, "this distribution. (See the file COPYING.) In that license\n");
  738.   fprintf(fp, "it is made clear that you are welcome to redistribute either\n");
  739.   fprintf(fp, "verbatim or modified copies of the program and the documentation\n");
  740.   fprintf(fp, "under certain conditions. Further you are told that this program\n");
  741.   fprintf(fp, "comes with ABSOLUTELY NO WARRANTY!\n");
  742. }
  743.  
  744.  
  745.  
  746.  
  747.  
  748. /**
  749. ***  This function displays the Usage() message.
  750. **/
  751. VOID Usage(VOID)
  752.  
  753. { fprintf(stderr, "Usage: IPDial SCRIPT,DEVICE/K,PROTOCOL/K,TERMINAL/S,ECHO/S,VERBOSE/S,HELP/S\n\n");
  754.   fprintf(stderr, "\tSCRIPT:    Script file to execute.\n");
  755.   fprintf(stderr, "\tDEVICE:    Device to use (default serial.device)\n");
  756.   fprintf(stderr, "\tPROTOCOL:  Protocol to use; XONXOFF, 7WIRE (default) or NONE)\n");
  757.   fprintf(stderr, "\tUNIT:      Unit to use (default 0)\n");
  758.   fprintf(stderr, "\tECHO:      Show modems replies.\n");
  759.   fprintf(stderr, "\tVERBOSE:   Be verbose.\n\n");
  760.   fprintf(stderr, "\tTERMINAL:  Run in terminal mode.\n");
  761.   fprintf(stderr, "\tHELP:      Print this message.\n");
  762.   fprintf(stderr, "\n\n%s  @ 1994 by Jochen Wiedmann\n\n", VString);
  763.   ShowGPL(stderr);
  764.   exit(5);
  765. }
  766.  
  767.  
  768.  
  769.  
  770.  
  771. /**
  772. ***  This is main().
  773. **/
  774. int main(int argc, char *argv[])
  775.  
  776. { struct
  777.   { STRPTR file;
  778.     STRPTR device;
  779.     STRPTR protocol;
  780.     LONG *unit;
  781.     ULONG terminal;
  782.     ULONG help;
  783.     ULONG echo;
  784.     ULONG verbose;
  785.   } args;
  786.  
  787.   args.file = NULL;
  788.   args.device = "serial.device";
  789.   args.protocol = "7wire";
  790.   args.unit = NULL;
  791.   args.terminal = FALSE;
  792.   args.echo = FALSE;
  793.   args.verbose = FALSE;
  794.   args.help = FALSE;
  795.  
  796.   if (!argc)    /*  No WB handling.     */
  797.   { exit(-1);
  798.   }
  799.  
  800.   if (atexit(Cleanup))
  801.   { fprintf(stderr, "Memory error.\n");
  802.     exit(20);
  803.   }
  804.  
  805.   if (!(MainRDArgs = ReadArgs("SCRIPT,DEVICE/K,PROTOCOL/K,UNIT/K/N,TERMINAL/S,"
  806.                   "HELP/S,ECHO/S,VERBOSE/S",
  807.                   (LONG *) &args, NULL)))
  808.   { fprintf(stderr, "Cannot parse command line.\n");
  809.   }
  810.  
  811.   if (args.help  ||
  812.       (!args.file  &&  !args.terminal))
  813.   { Usage();
  814.   }
  815.  
  816.   EchoMode = args.echo;
  817.   if (args.verbose)
  818.   { VerboseMode = TRUE;
  819.     printf("%s  @ 1994 by Jochen Wiedmann\n\n", VString);
  820.     ShowGPL(stdout);
  821.     printf("\n\n");
  822.   }
  823.  
  824.   if (args.terminal)
  825.   { SerialOpen(args.device, args.protocol, args.unit ? *args.unit : 0);
  826.     SerialTerminal();
  827.   }
  828.   else if (args.file)
  829.   { ParseFile(args.file);
  830.     ProcessFile();
  831.   }
  832.  
  833.   exit(0);
  834. }
  835.